package com.voja.mrvica2.db;

import java.util.ArrayList;
import java.util.Calendar;

import com.voja.mrvica2.App;
import com.voja.mrvica2.Entry;

import android.content.Context;

public class EntryItem extends DB {
    public int itemsPerPage = 10;

    public EntryItem(Context context) {
        super(context);
    }
    
    public ArrayList<Entry> get(int continentId, int page) {
        ArrayList<Entry> result = new ArrayList<Entry>();
        
        int offset = (page - 1) * itemsPerPage;
        offset = offset > 0 ? offset : 0;
        cursor = query( String.format("SELECT * FROM countries " + 
                                      "WHERE continent_id=%d " +
                                      "ORDER BY name " +
                                      "LIMIT %d, %d",
                                      continentId, offset, itemsPerPage) );
        if (cursor != null) {
            Entry e;
            while (cursor.moveToNext()) {
                e = new Entry();
                e.set(
                    cursor.getInt(cursor.getColumnIndex("id")),
                    cursor.getString(cursor.getColumnIndex("name")),
                    cursor.getString(cursor.getColumnIndex("image")),
                    cursor.getString(cursor.getColumnIndex("link")),
                    cursor.getString(cursor.getColumnIndex("description"))
                );
                result.add(e);
            }
        }
        
        return result;
    }
    
    public boolean expired(int continentId) {
        long    lastStamp = getLastUpdateTimeStamp(continentId);
        long    now       = Calendar.getInstance().getTimeInMillis();
        
        return (lastStamp + App.CACHE_LIFETIME) < now;
    }
    
    public long getLastUpdateTimeStamp(int continentId) {
        long result = 0;
        cursor = query("SELECT stamp FROM sync WHERE key='continent_" + continentId + "';");
        if (cursor != null) {
            while (cursor.moveToNext()) {
                result = cursor.getLong(0);
            }
        }
        return result;
    }
    
    public boolean save(int continentId, ArrayList<Entry> data) {
        boolean ret = true;
        
        // insert continents
        Entry e;
        String sql;
        for (int i=0; i<data.size(); i++) {
            e = data.get(i);
            
            // insert if there's no country
            sql = String.format("INSERT OR IGNORE INTO countries(id, continent_id, name) VALUES(%d, 0, '')", 
                                e.getId());
            execute(sql);
            
            // update country data
            sql = String.format("UPDATE countries " +
                                "SET continent_id=%d, name='%s', image='%s', link='%s', description='%s' " +
                                "WHERE id=%d", 
                                continentId, e.getTitle(), e.getImage(), 
                                e.getLink(), prepForSave(e.getDescription()), e.getId());
            ret = ret && (execute(sql)>0);
        }
        
        // update sync TimeStamp
        if (ret) {
            execute("INSERT OR IGNORE INTO sync(key, stamp) VALUES('continent_" + continentId + "', 0)");
            sql = String.format("UPDATE sync SET stamp=%s WHERE key='continent_" + continentId + "';",
                                Calendar.getInstance().getTimeInMillis());
            ret = ret && (execute(sql)>0);
        }
        
        return ret;
    }
}
